// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    currentTimeStamp: 0,
    num:[],
    openid:"",
    name:"",
    phone:"",
    id:"",
    laoren:[],
    total_walk:"",
    device_code:"",
    diedao_alarm:'跌倒报警',
    jiuzuo_text:"未久坐",
    sbxx:"",
    index1:0,
  },
  onLoad: function () {
    const that=this
    const app =getApp()
    app.fetchData().then(data => {
      // 数据请求成功，可以在这里进行页面加载操作
      that.setData({
        openid:app.globalData.openid
      })
    })
    let currentTimeStamp = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8)
    this.setData({
      currentTimeStamp: currentTimeStamp,
    });
      // 导航栏
      // 1.获取屏幕可使用宽度
      let windowWidth = wx.getSystemInfoSync().windowWidth;
      // 2.获取状态栏高度
      const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
      //3.获取胶囊按钮位置信息
      const menuButton = wx.getMenuButtonBoundingClientRect();
      this.setData({
        navtop:menuButton.top
      })
  },
  onShow(){
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      mask: true 
    });
    this.getDate2()
  },
  getinfo(e){
    const itemId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detali/detali?id=' + itemId
    })
  },
  gitfor(){
    wx.switchTab({
      url: '/pages/Member/Member',
    })
  },
  bangding(){
    wx.navigateTo({
      url: '../oldcard/oldcard',
    })
  },
  getDate2(){
    const that = this
    const app =getApp()
    this.setData({
      openid:app.globalData.openid
    })
    console.log(app.globalData.openid);
        wx.request({
          url: 'https://dj.ifankui.com/api/usrEn/deviceList',
          method:"POST",
          data:{
            openid:this.data.openid
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success:(res)=>{
            console.log(res);
            let p =[]
            let p1 =[]
            const a=res.data.data
            a.forEach(element => {
              p.push(element)
              p1.push(element.device_code)
            });
              this.setData({
                sbxx:p,
                show:p1
              },()=>{
                wx.request({
                  url: 'https://dj.ifankui.com/api/device/getDeviceInfo',
                  method:"POST",
                  data:{
                    openid:this.data.openid,
                    // device_code:"898604B0102140204004",
                    device_code:this.data.sbxx[that.data.index1].device_code,
                  },
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success:(res)=>{
                    console.log(res);
                    console.log(this.data.sbxx);
                    let ppp={}
                    console.log(res.data.device.jiuzuo_alarm[0]);
                    if(!res.data.device.jiuzuo_alarm[0]){
                        ppp=1
                    }
                    else {
                      ppp = res.data.device.jiuzuo_alarm[0];
                    }
                    console.log(ppp);
                    that.setData({
                      Batterysvalue: res.data.device.battery,
                      device_code: res.data.device.device_code,
                      total_walk: res.data.device.total_walk,
                      diedao_alarm: res.data.device.diedao_alarm,
                      jiuzuo_alarm: ppp,
                      dataLoaded:true
                    }, () => {
                      const inputDate = that.data.jiuzuo_alarm.dt
                      console.log(inputDate);
                      function inputDate11(inputDate) {
                        if(inputDate){
                          let inputDate1 = inputDate.replace(/-/g, '/');
                          return inputDate1
                        }
                        else {
                          let inputDate1 = ""
                          return inputDate1
                        }
                      }
                      let inputDate1=inputDate11(inputDate)
                      const date = new Date(inputDate1);
                      // 获取当前时间
                      const now = new Date();
                      console.log(now,date);
                      // 计算时间差（单位：毫秒）
                      const diffInMillis = now - date;
                      // 将毫秒转换为分钟
                      const diffInMinutes = Math.floor(diffInMillis / (1000 * 60));
                      // 计算小时数
                      const hours = Math.floor(diffInMinutes / 60);
                      // 计算剩余分钟数
                      const minutes = diffInMinutes % 60;
                      that.setData({
                        jiuzuo_text:hours+"小时"+minutes+"分"+"前"
                      })
                      
                      let percentageNumber = parseFloat(res.data.device.battery.replace('%', ''));
                      console.log(percentageNumber);
                      if (percentageNumber < 20) {
      
                        wx.showModal({
                          title: '警告',
                          content: '当前电量低于20%',
                          showCancel: false, // 是否显示取消按钮
                          confirmText: '确定', // 确定按钮的文字
                          confirmColor: '#fb3660', // 确定按钮的文字颜色
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定');
                              // 用户点击确定后的操作
                            } else if (res.cancel) {
                              console.log('用户点击取消');
                              // 用户点击取消后的操作
                            }
                          }
                        });
                      }
                    })
          }
        })
      })
    }
  })
  },
  showActionSheet() {
    const that = this
    wx.showActionSheet({
      itemList:that.data.show,
      success(res) {
        console.log(res.tapIndex) // 用户点击的按钮序号，从0开始
        that.setData({
          index1:res.tapIndex,
        })
        that.getDate2()
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  }
})

